package GUI;

import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PanelCal extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTable jTable1;
	private JScrollPane jScrollPane1;
	private JLabel jLabel1;
	private final String days[]={"L","M","X","J","V","S","D"};
	private final String months[]={"Enero","Febrero","Marzo","ABril","Mayo",
									"Junio","Julio","Agosto","Septiembre",
									"Octubre","Noviembre","Diciembre"};
	private final int daysMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
	
	private int month;
	private int year;
	private String[][]calendario=new String[5][7];
	
	/**
	 * This is the default constructor
	 */
	public PanelCal() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(149, 162);
		FlowLayout thisLayout = new FlowLayout();
		this.setLayout(thisLayout);
		this.setPreferredSize(new java.awt.Dimension(266, 238));
		{
			jLabel1 = new JLabel("dd Mes aaa",JLabel.CENTER);
			this.add(jLabel1);
			jLabel1.setText("dd Mes aaa");
			jLabel1.setPreferredSize(new java.awt.Dimension(180, 20));
		}
		{
			jScrollPane1 = new JScrollPane();
			this.add(jScrollPane1);
			jScrollPane1.setPreferredSize(new java.awt.Dimension(221, 174));
			jScrollPane1.setBackground(new java.awt.Color(81,168,255));
			{
				TableModel jTable1Model = 
					new DefaultTableModel(
							new String[][] { { "","","","","","","" }, { "","","","","","","" },
									{ "","","","","","","" },{ "","","","","","","" },{ "","","","","","","" }},
									new String[]{"L","M","X","J","V","S","D"});
				
			jTable1 = new JTable();
			jScrollPane1.setViewportView(jTable1);
			jTable1.setModel(jTable1Model);
			jTable1.setPreferredSize(new java.awt.Dimension(160, 149));
			jTable1.setRowHeight(30);
			jTable1.setSize(180, 180);
			}
		}
		
		
        int offset = 0;
        int numDays = 0;
        int today = 0;
        
        
        
        /**
         * GregorianCalendar es una subclase de Calendar, por lo que nos 
         * proporciona toda la funcionalidad de esta clase y ademas nos brinda 
         * algunos metodos mas. En este caso estamos iniciando dos 
         * GregorianCalendar utilizando diferentes constructores. 
         *
         * currCal se inicia con Calendar.getInstance() que nos devuelve un 
         *         Calendar fijado en el dia de hoy. 
         * cal     se inicia con el constructor que recibe la fecha 
         */
        Calendar cal = new GregorianCalendar();
        today = cal.get(Calendar.DAY_OF_MONTH);
		month =cal.get(Calendar.MONTH);
		year =cal.get(Calendar.YEAR);
		jLabel1.setText(String.valueOf(today)+" "+months[month]+" "+String.valueOf(year));
				
        
        /** 
         * Obtengo el numero de dias del arreglo 
         */
        numDays = daysMonth[month];
        
        /** Si es biciesto y es febrero le sumo uno al numero de dias */
        if (month == Calendar.FEBRUARY && ((GregorianCalendar) cal).isLeapYear(year)) {
            numDays++;
        }
        
        /** 
         * Obtengo el numero de dias que van a ir en blanco.
         * Al llamar el metodo get de la siguiente manera 
         * get(Calendar.DAY_OF_WEEK) nos va a devolver un 0 para domingo, un 1 
         * para lunes y un 6 para sabado, por lo que es el numero de dias que 
         * debemos dejar en blanco directamente 
         * Si queremos que la semana empiece en lunes , le restamos uno.
         */
        offset = (cal.get(Calendar.DAY_OF_WEEK))%7;
        
        //creo la matriz
        int n=0,d=1;
        
        for (int i = 0; i < 5; i++) {
        	for (int j = 0; j < 7; j++) {
        		if(n<offset){
        			calendario[i][j]=" ";
            	}
            	else if(d<=numDays){
            		calendario[i][j]=String.valueOf(d);
            		if(d==today){
            			calendario[i][j]="<"+String.valueOf(d)+">";
            		}
            		d++;
            	}
            	else{
            		calendario[i][j]=" ";
            	}
        		n++;
			}
		}
		
        TableModel jTable1Model = new DefaultTableModel(calendario,
							days);
        jTable1.setModel(jTable1Model);
        jTable1.setShowHorizontalLines(false);
        jTable1.setShowVerticalLines(false);
        jTable1.setRowSelectionAllowed(false);
        jTable1.setBackground(new java.awt.Color(218,218,218));

	}

}  //  @jve:decl-index=0:visual-constraint="10,51"
